package logic

import (
	"context"
	"selection-inventory/application/applet/internal/model"

	"selection-inventory/application/applet/internal/svc"
	"selection-inventory/application/applet/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type CreateCommodityLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewCreateCommodityLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateCommodityLogic {
	return &CreateCommodityLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *CreateCommodityLogic) CreateCommodity(req *types.CreateRequest) (resp int64, err error) {
	commodity := &model.Commodity{
		Title:   req.Title,
		Picture: req.Picture,
		Ali:     req.Ali,
		Douyin:  req.Douyin,
		Pdd:     req.Pdd,
		Taobao:  req.Taobao,
	}
	tx := l.svcCtx.DB.Create(commodity)
	if tx.Error != nil {
		return 0, err
	}
	return commodity.Id, nil
}
